package org.x.lawyermod.service.impl;


import ch.qos.logback.core.util.StringUtil;


import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.x.commonmod.config.BusinessException;
import org.x.lawyermod.dto.LawyerListDTO;
import org.x.lawyermod.dto.LawyerUpdateDTO;
import org.x.lawyermod.vo.LawyerListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.x.lawyermod.service.ILawyerService;
import org.x.lawyermod.entity.LawyerEntity;
import org.x.lawyermod.mapper.LawyerMapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;

/**
 * 律师信息表 服务层实现。
 *
 * @author mybatis-flex-helper automatic generation
 * @since 1.0
 */
@Service
public class LawyerServiceImpl extends ServiceImpl<LawyerMapper, LawyerEntity> implements ILawyerService {

    @Autowired
    private LawyerMapper lawyerMapper;


    @Override
    public Page<LawyerListVO> pagelist(LawyerListDTO lawyerListDTO) {
        //检查lawyerListDTO是否为空，不为空才构建查询条件
        QueryWrapper wrapper = new QueryWrapper();
        if(!StringUtil.isNullOrEmpty(lawyerListDTO.getLicenseNo()))
        {
            wrapper.like(LawyerEntity::getLicenseNo, lawyerListDTO.getLicenseNo());
        }
        if(!StringUtil.isNullOrEmpty(lawyerListDTO.getSpecialty()))
        {
            wrapper.like(LawyerEntity::getSpecialty, lawyerListDTO.getSpecialty());
        }
        if(!StringUtil.isNullOrEmpty(lawyerListDTO.getServiceArea()))
        {
            wrapper.like(LawyerEntity::getServiceArea, lawyerListDTO.getServiceArea());
        }
        if(!StringUtil.isNullOrEmpty(String.valueOf(lawyerListDTO.getIsCertified())))
        {
            wrapper.like(LawyerEntity::getIsCertified, lawyerListDTO.getIsCertified());
        }

        Page<LawyerEntity> page = lawyerMapper.paginate(
                lawyerListDTO.getPage(),
                lawyerListDTO.getSize(),
                 wrapper
        );
        Page<LawyerListVO> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        return pageVo;


    }

    @Override
    public int updateById(LawyerUpdateDTO lawyerUpdateDTO) {
        //检查律师ID是否存在
        LawyerEntity lawyer = lawyerMapper.selectOneByQuery(new QueryWrapper().eq("id", lawyerUpdateDTO.getId()));
        if (lawyer == null) {
            throw new BusinessException("律师不存在");
        }
        BeanUtils.copyProperties(lawyerUpdateDTO, lawyer);
        lawyerMapper.insertOrUpdateSelective(lawyer);
        return 0;

    }
}